package cn.gson.springboot.controller.privilegecontroller;

import cn.gson.springboot.model.pjos.Dept;
import cn.gson.springboot.model.pjos.Permissionsset;
import cn.gson.springboot.model.pjos.Position;
import cn.gson.springboot.model.service.privilegeservice.DeptService;
import cn.gson.springboot.model.vo.privilegevo.DeptVo;
import cn.gson.springboot.model.vo.privilegevo.PositionVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/dept")
public class DeptController {

    @Autowired
    DeptService deptService;


    /**
     * 递归查询部门及下级部门
     * @return
     */
    @RequestMapping("/deptTree")
    @ResponseBody
    public List<DeptVo> deptTree(){
        return deptService.deptTree();
    }


    /*根据部门id查询职务*/
    @RequestMapping("/positionTree")
    @ResponseBody
    public List<PositionVo> positionTree(int deptId){
        return deptService.positionTree(deptId);
    }

    /*save部门*/
    @RequestMapping("/saveDept")
    @ResponseBody
    public void saveDept(@RequestBody Dept dept){
        deptService.saveDept(dept);
    }

    /*所有权限组*/
    @RequestMapping("/permissionGroup")
    @ResponseBody
    public Map<String,Object> permissionGroup(Integer deptId){
        Map map = new HashMap();
        List<Permissionsset> permissionssetList= deptService.permissionGroup();//所有权限组
        List<Integer> deptPermissionssetList=deptService.deptPermissionGroup(deptId);//部门已拥有的权限组
        map.put("permissionssetList",permissionssetList);
        map.put("deptPermissionssetList",deptPermissionssetList);
        return map;
    }

    /*save部门权限组*/
    @RequestMapping("/save_permission")
    @ResponseBody
    public void savePermissionsset(String perminssionsset){
        deptService.savePermissionsset(perminssionsset);
    }

    /*查询部门拥有的权限组，权限组里拥有的子权限*/
    @RequestMapping("/deptMeun")
    @ResponseBody
    public  Map<String,Object> deptMeun(Integer deptId, Integer postId){
        List<Permissionsset> permissionssetList =deptService.deptMeun(deptId);
        List<Integer> positionList = deptService.positionMeun(postId);
        Map map = new HashMap();
        map.put("permissionssetList",permissionssetList);
        map.put("positionList",positionList);
        return map;
    }

    /*授权职务*/
    @RequestMapping("/savePositionMeun")
    @ResponseBody
    public  void savePositionMeun(String positionMeun){
        deptService.savePositionMeun(positionMeun);
    }

   /* 删除职务*/
     @RequestMapping("/deleteDept")
     @ResponseBody
    public int deleteDept(Integer deptId){
        return  deptService.deleteDept(deptId);
    }

    /**新增职务*/
    @RequestMapping("/add_Position")
    @ResponseBody
    public void addPosition(String positionList){
        deptService.addPosition(positionList);
    }

    /**修改职务*/
    @RequestMapping("/update_Position")
    @ResponseBody
    public void updatePosition(@RequestBody Position position){
        deptService.updatePosition(position);
    }

    /**删除职务*/
    @RequestMapping("/delete_Position")
    @ResponseBody
    public int deletePosition(Integer postId){
        return deptService.deletePosition(postId);
    }

    /* 用于查询部门名称是否存在*/
    @RequestMapping("/findDeptNameIsNo")
    @ResponseBody
    public int findDeptIsNo(String deptName){
        return  deptService.findDeptIsNo(deptName);
    }

    /*用于查询职务下的名称是否存在*/
    @RequestMapping("/findPostNameIsNo")
    @ResponseBody
    public int findPositionIsNo(Integer deptId,  String postName){
        return deptService.findPositionIsNo(deptId,postName);
    }

    /**
     * 设置一级角色或取消一级角色
     * @param position
     */
    @RequestMapping("/set_position_level")
    @ResponseBody
    public void setPostLevel(String position){
        deptService.setPostLevel(position);
    }
}
